package com.rerun.phone.memberuser;

import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.common.rabbitmq.Consumer;
import com.rerun.common.rabbitmq.RabbitMqConsumer;
import com.rerun.common.rabbitmq.RabbitMqProducer;
import com.rerun.entity.YoyoPark;
import com.rerun.entity.vo.ParkListShowVo;
import com.rerun.service.IJobService;
import com.rerun.service.IYoyoParkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.amqp.core.MessageListener;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * The type Park controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/29 19:56
 */
@Api(value = "求职端-幼儿园相关", description = "求职端-幼儿园相关")
@RestController
@RequestMapping("/memberApi/park")
public class ParkController extends SuperController {
    @Resource
    private IYoyoParkService iYoyoParkService;
    @Resource
    private IJobService iJobService;
    @Resource
    RabbitMqProducer rabbitMqProducer;
    @Resource
    RabbitMqConsumer rabbitMqConsumer;

    /**
     * 获取某个幼儿园的信息.
     *
     * @param id the id
     * @return the park info by id
     */
    @ApiOperation(value = "幼儿园信息", notes = "获取某个幼儿园的信息", httpMethod = "GET")
    @RequestMapping(value = "/parkInfo/{id}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest getParkInfoById(@ApiParam(required = true, name = "id", value = "幼儿园id") @PathVariable("id") String id) {
        YoyoPark park = iYoyoParkService.selectById(id);
        park.setJobList(iJobService.findAvailableJobByParkId(id));
        return Rest.okData(park);
    }


    /**
     * Gets all park.
     *
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @param parkName   the park name
     * @return the all park
     */
    @ApiOperation(value = "幼儿园列表", notes = "分页获取幼儿园列表", httpMethod = "GET")
    @RequestMapping(value = "/list/{pageNumber}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest getParkListByParams(@ApiParam(required = true, name = "pageNumber", value = "页码") @PathVariable("pageNumber") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize, String parkName) {
        Page<YoyoPark> parkPage = getPage(pageNumber, pageSize);
        Page<YoyoPark> data = iYoyoParkService.selectListByParams(parkPage, parkName);
  /*      rabbitMqProducer.setAutoDelete(false);
        rabbitMqProducer.setDurable(true);
        rabbitMqProducer.setExchangeName("T_exchange_park");
        rabbitMqProducer.setQueueName("T_queue_park");
        rabbitMqProducer.setExchangeTypeEnum(ExchangeTypeEnum.TOPIC);
        rabbitMqProducer.setRoutingKey("T_routingKey_park");
        rabbitMqProducer.admin();*/
        rabbitMqProducer.send("T_exchange_park", "T_routingKey_park", data);
        MessageListener consumer = new Consumer();

        rabbitMqConsumer.setMessageConsumer(consumer);
        rabbitMqConsumer.setOnOfConsumer(5);
        rabbitMqConsumer.setQueueName("T_queue_park");
        rabbitMqConsumer.setRoutingKey("T_routingKey_park");
        try {
            rabbitMqConsumer.initialize();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rest.okData(data);
    }

    /**
     * 展示园所列表信息
     *
     * @param map        the map
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @return the rest
     * @deprecated
     */
    @ApiOperation(value = "园所列表", notes = "分页获取幼儿园列表(废弃)", httpMethod = "GET")
    @RequestMapping(value = "/showParkList/{pageNumber}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest showParkList(@RequestParam(defaultValue = "") Map<String, Object> map, @ApiParam(required = true, name = "pageNumber", value = "页码") @PathVariable("pageNumber") Integer pageNumber,
                             @RequestParam(defaultValue = "15") Integer pageSize) {

        List<ParkListShowVo> parkList = iYoyoParkService.showParkList(map, pageNumber, pageSize);
        return Rest.okData(parkList);
    }


    /**
     * 最近查看过我简历的幼儿园的不重复5条记录.
     *
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "谁看过我", notes = "最近查看过我的幼儿园的不重复5条记录 {memberId=08d30f5fdbe943d0be6728fe283c9e99}", httpMethod = "GET")
    @RequestMapping(value = "/list/watchedParks/{memberId}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findWatchedParks(@PathVariable("memberId") String memberId) {

        return Rest.okData(iYoyoParkService.selectListWatchedMe(memberId));
    }

    /**
     * 收藏我的.
     *
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "对我感兴趣", notes = "收藏我的 memberId=c79ba431f9f74dfbae585b87b0cde933", httpMethod = "GET")
    @RequestMapping(value = "/list/collectionMe/{memberId}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findParksCollectionMe(@PathVariable("memberId") String memberId) {
        return Rest.okData(iYoyoParkService.selectListCollectionMe(memberId));
    }
}
